package com.pro.springbootshop.controller;

import com.pro.springbootshop.entity.FlashActivity;
import com.pro.springbootshop.entity.FlashEntry;
import com.pro.springbootshop.entityExtend.FlashActivityReturn;
import com.pro.springbootshop.entityExtend.FlashEntryReturn;
import com.pro.springbootshop.service.FlashActivityService;
import com.pro.springbootshop.service.FlashEntryService;
import com.pro.springbootshop.util.TokenUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FlashEntryController {
    @Autowired
    TokenUtil tokenUtil;
    @Autowired
    FlashEntryService flashEntryService;
    @Autowired
    FlashActivityService flashActivityService;
    @ApiOperation("提交报名表")
    @PostMapping("/api/flashEntry/insert")
    public String saveFlashEntry(@RequestBody FlashEntry flashEntry, HttpServletRequest request){
        String token = request.getHeader("token");
        return flashEntryService.saveFlashEntry(flashEntry,token);
    }
    @ApiOperation("商家根据活动id查询所有用户的id")
    @GetMapping("/api/flashEntry/{page}/{limit}/{sellerId}")
    public FlashEntryReturn getAllFlashEntry(@PathVariable(name = "page") Integer page, @PathVariable(name = "limit") Integer limit,@PathVariable(name = "sellerId") String sellerId){
//        String token = request.getHeader("token");
        FlashActivityReturn flashActivityReturn=flashActivityService.sellerGetAllFlashActivity(page,limit,sellerId);
        List<FlashActivity> flashActivityList=flashActivityReturn.getFlashActivityList();
        System.out.println("!!!!!!!!!!!!!!!!!"+flashActivityList.toString());
        FlashEntryReturn flashEntryReturn=null;
        List<FlashEntry> flashEntryList=new ArrayList<>();
//        List<FlashEntry> flashEntryList1=null;
        int total=0;
        for(FlashActivity flashActivity:flashActivityList){
//            flashEntryList1=flashEntryService.getAllFlashEntry(page,limit,flashActivity.getFlashActivityId()).getFlashEntryList();
//            for(FlashEntry flashEntry:flashEntryList1) {
//                flashEntryList.add(flashEntry);
//            }
            flashEntryList.addAll(flashEntryService.getAllFlashEntry(page,limit,flashActivity.getFlashActivityId()).getFlashEntryList());
            System.out.println("!!!!!!!!!!!!!!!!!"+flashEntryService.getAllFlashEntry(page,limit,flashActivity.getFlashActivityId()).getFlashEntryList().toString());
            total=total+flashEntryService.getAllFlashEntry(page,limit,flashActivity.getFlashActivityId()).getTotal();
        }
        return new FlashEntryReturn(flashEntryList,total);
    }
    @ApiOperation("管理员查询所有用户的id")
    @GetMapping("/api/flashEntry_admian/{page}/{limit}")
    public FlashEntryReturn getAllFlashEntry_admian(@PathVariable(name = "page") Integer page, @PathVariable(name = "limit") Integer limit, HttpServletRequest request){

        return  flashEntryService.getAllFlashEntry_admin(page,limit);
    }

    @ApiOperation("根据活动id,用户id查询是否有用户自己的id")
    @GetMapping("/api/flashEntry/{flashActivityId}")
    public String buyergetFlashEntry( @PathVariable(name = "flashActivityId") Integer flashActivityId, HttpServletRequest request){
        String token = request.getHeader("token");
        return flashEntryService.buyergetFlashEntry(token,flashActivityId);
    }
    @ApiOperation("根据用户id查询所有用户报名")
    @GetMapping("/api/flashEntry/{page}/{limit}")
    public FlashEntryReturn buyerIdGetFlashEntry( @PathVariable(name = "page") Integer page, @PathVariable(name = "limit") Integer limit, HttpServletRequest request) {
        String token = request.getHeader("token");
        return flashEntryService.buyerIdGetFlashEntry(page,limit,token);
    }
    @ApiOperation("修改报名表的购买状态")
    @GetMapping("/api/flashEntry/change/{flashEntryId}/{buyState}")
    public String changeFlashEntry(@PathVariable(name = "flashEntryId") Integer flashEntryId, @PathVariable(name = "buyState") Integer buyState){
        return flashEntryService.changeFlashEntry(flashEntryId,buyState);
    }
}